package com.tentative.competition.web.controller.schedule.service;

import com.tentative.competition.common.core.controller.BaseController;
import com.tentative.competition.common.core.domain.AjaxResult;
import com.tentative.competition.common.core.result.ApiResult;
import com.tentative.competition.common.core.result.PageResult;
import com.tentative.competition.common.model.schedule.param.ScheduleAdminPageParam;
import com.tentative.competition.common.model.schedule.param.ScheduleAdminParam;
import com.tentative.competition.common.model.schedule.param.ScheduleNoticeParam;
import com.tentative.competition.common.model.schedule.resp.ScheduleAdminResp;
import com.tentative.competition.common.utils.StringUtils;
import com.tentative.competition.schedule.domain.ScheduleAdmin;
import com.tentative.competition.schedule.service.ScheduleAdminService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import java.util.List;

/**
 * @auther: Smile Huang
 * @date: 2023/6/22 22:03
 * @desc: 赛程管理
 * @version: 2.0
 */
@Api(tags ="service-赛程管理")
@RestController
@RequestMapping("/service/schedule")
public class ScheduleController extends BaseController {

    @Autowired
    private ScheduleAdminService scheduleAdminService;

    @ApiOperation("查询缩略的赛程列表")
    @GetMapping("/abridgeList")
    public ApiResult abridgeList() {
        List<ScheduleAdmin> scheduleAdmins = scheduleAdminService.lambdaQuery().eq(ScheduleAdmin::getDelFlag,0)
                .select(ScheduleAdmin::getId,ScheduleAdmin::getScheduleTitle,ScheduleAdmin::getScheduleLabel)
                .list();
        scheduleAdmins.stream().forEach(e->e.setScheduleTitle(e.getScheduleTitle() + " " + e.getScheduleLabel()));
        return ApiResult.ok(scheduleAdmins);
    }

    @ApiOperation("查询赛程列表-分页")
    @GetMapping("/list")
    public ApiResult list(ScheduleAdminPageParam param) {
        PageResult<ScheduleAdminResp> pageResult = scheduleAdminService.pageList(param);
        return ApiResult.ofPage(pageResult.getContent(),pageResult.getTotal());
    }

    /**
     * 创建｜修改赛程信息
     * @param param
     * @return
     */
    @ApiOperation("创建｜修改赛程信息")
    @PostMapping("/add-edit")
    public AjaxResult addOrEdit(@RequestBody ScheduleAdminParam param) {
        scheduleAdminService.addOrEdit(param);
        return AjaxResult.success();
    }

    /**
     * 设置公告
     * @param param
     * @return
     */
    @ApiOperation("设置公告")
    @PostMapping("/edit-notice")
    public AjaxResult editNotice(@RequestBody @Validated ScheduleNoticeParam param) {
        if(StringUtils.isNotEmpty(param.getType()) && param.getType().equals("user")){
            scheduleAdminService.lambdaUpdate()
                    .eq(ScheduleAdmin::getId, param.getId())
                    .set(ScheduleAdmin::getUserNotice, param.getUserNotice())
                    .update();
        }else{
            scheduleAdminService.lambdaUpdate()
                    .eq(ScheduleAdmin::getId, param.getId())
                    .set(ScheduleAdmin::getScheduleNotice, param.getScheduleNotice())
                    .update();
        }
        return AjaxResult.success();
    }

    @ApiOperation("获取赛程明细")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(scheduleAdminService.getInfo(id));
    }

    @ApiOperation("获取当前赛程")
    @GetMapping(value = "/getNow")
    public AjaxResult getNow() {
        return success(scheduleAdminService.getNow());
    }


    @ApiOperation("删除赛程")
    //@PreAuthorize("@ss.hasPermi('pc:flow:remove')")
    @PostMapping("/{ids}")
    public AjaxResult remove(@PathVariable List<Long> ids) {
        return toAjax(scheduleAdminService.deleteSchedule(ids));
    }


    @ApiImplicitParams({
            @ApiImplicitParam(name = "scheduleStatus", value = "赛程状态(UNDERWAY:进行中，ACCOMPLISH：已完成)", dataType = "String", dataTypeClass = String.class,required = true ),
            @ApiImplicitParam(name = "scheduleId", value = "赛程ID", dataType = "Long", dataTypeClass = Long.class,required = true ),
    })
    @ApiOperation("启用停用赛程")
    @GetMapping(value = "/edit-enable")
    public AjaxResult editEnable(@RequestParam(value = "scheduleStatus") String scheduleStatus,
                                          @RequestParam(value = "scheduleId") Long scheduleId) {
        return success(scheduleAdminService.editEnable(scheduleStatus,scheduleId));
    }

}
